package demo.model;


import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author ：jiangHaoTing
 * @date ：2022/4/1 14:24
 */
public class A {


    public static void main(String[] args) {

        String urlToRedirect =   String.format("https://sso.zxxk.com" + "/login?service=%s", "http://www.zxxk.com/");
        System.out.println(urlToRedirect);

        String url = "https://sso.zxxk.com" + "/oauth2/authorize";
        String openId ="open-335-uVqrA0xJJW";
        try {
        openId = aesEncrypt(openId, "aa56b1e2c30a4a4c9520704394f9371d");
        String timeSpan =aesEncrypt(getTimeStamp(), "aa56b1e2c30a4a4c9520704394f9371d");

        SortedMap<String, Object> paramMap = new TreeMap<>();
        paramMap.put("client_id", "zxxk1208G03N2PAUHL");
        paramMap.put("open_id", openId);
        paramMap.put("service", "http://www.zxxk.com/");
        paramMap.put("redirect_uri", "http://cloud.leke.cn/auth/common/oauth/authentication.htm");
        paramMap.put("timespan", timeSpan);
        paramMap.put("extra", StringUtils.isEmpty("13940863447") ? "" : "13940863447");
        paramMap.put("signature", generateSignature(paramMap, "aa56b1e2c30a4a4c9520704394f9371d"));
        //加密之后参数中会含有特殊字符，如加号“+”，会导致浏览器url解析成空格，因此需要给参数值编一下码
        String S = url + "?" + getEncodedParamStr(paramMap);
        System.out.println(S);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static String getEncodedParamStr(Map<String, Object> paramMap) throws UnsupportedEncodingException {
        StringBuilder encodedParamStr = new StringBuilder();
        for (Map.Entry param : paramMap.entrySet()) {
            if (encodedParamStr.length() > 0) {
                encodedParamStr.append("&");
            }
            encodedParamStr.append(param.getKey()).append("=").append(URLEncoder.encode(param.getValue().toString(), "utf-8"));
        }
        return encodedParamStr.toString();
    }

    public static String generateSignature(SortedMap<String, Object> param, String secret) {
        if (isEmpty(param)) {
            return "";
        }
        StringBuilder paramValueStr = new StringBuilder();
        for (String item : param.keySet()) {
            paramValueStr.append(param.get(item));
        }
        paramValueStr.append(secret);
        return md5(paramValueStr.toString());
    }

    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    public static String md5(String encryptStr){
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            md.update(encryptStr.getBytes("utf-8"));
            byte[] bPwd=md.digest();
            String pwd = new BigInteger(1, bPwd) .toString(16);
            if(pwd.length()%2==1){
                pwd = "0" + pwd;
            }
            return pwd;
        } catch (Exception var3) {
            throw new RuntimeException(var3);
        }
    }

    private static String getTimeStamp() {
        return String.valueOf(System.currentTimeMillis());
    }

    public static String aesEncrypt(String str, String key) throws Exception {
        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(key)){
            return "";
        }
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        return new BASE64Encoder().encode(bytes);
    }
}
